package com.csd.dao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.pojo.Team;

/**
 * A data access object (DAO) providing persistence and search support for Team
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see .Team
 * @author MyEclipse Persistence Tools
 */

public class TeamDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(TeamDAO.class);
	// property constants
	public static final String TEAMNUM = "teamnum";
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String INTRODUCTION = "introduction";

	protected void initDao() {
		// do nothing
	}

	public void save(Team transientInstance) {
		log.debug("saving Team instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(Team transientInstance) {
		log.debug("updating Team instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(Team persistentInstance) {
		log.debug("deleting Team instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Team findById(Integer id) {
		log.debug("getting Team instance with id: " + id);
		try {
			Team instance = (Team) getHibernateTemplate().get(Team.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/** 通过项目ID得到团队 */
	public Team findByProjectID(Integer projectID) {
		Team team = null;
		String queryString = "from Team t where t.project.id = ?";
		team = (Team) this.getHibernateTemplate().find(queryString, projectID);
		return team;
	}

	public List findByExample(Team instance) {
		log.debug("finding Team instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Team instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Team as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTeamnum(Object teamnum) {
		return findByProperty(TEAMNUM, teamnum);
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByIntroduction(Object introduction) {
		return findByProperty(INTRODUCTION, introduction);
	}

	public List findAll() {
		log.debug("finding all Team instances");
		try {
			String queryString = "from Team";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Team merge(Team detachedInstance) {
		log.debug("merging Team instance");
		try {
			Team result = (Team) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Team instance) {
		log.debug("attaching dirty Team instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Team instance) {
		log.debug("attaching clean Team instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public boolean hasProjectOrNot(Integer id) {
		
		log.debug("attaching check having applied project or not");
		try {
			List<Team> team = null;
			String queryString = "from Project p where p.team.id = ?";
			team = (List<Team>)this.getHibernateTemplate().find(queryString, id);
			if (team.size() != 0)
				return true;
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
		return false;
	}

	public static TeamDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TeamDAO) ctx.getBean("TeamDAO");
	}
	public int getTeamMemberNumberByID(Integer ID)
	{
		List<Team> team = null;
		String queryString = "from Student s where s.team.id = ?";
		team = (List<Team>)this.getHibernateTemplate().find(queryString, ID);
		return team.size();
		
	}
}